<?php
/**
 * 天马供应商SDK
 * @author 陈韵
 */
namespace App\Service\VendorClient\Shoptianma;
use App\Service\Code\Normal;
use App\Service\TaskServer\TaskWorker;
use Service\AppService\IdService;
use Hyperf\Utils\WaitGroup;
use App\Model\CommodityModel\Tianmagoods;
use App\Model\CommodityModel\SupplierCommodity;

class ShoptianmaApiClient
{
    private $account  = 'Shoptianma';
    protected $config;
    protected $RouteMap = [
        'queryGoodsDetail'      => '/api/openapi/queryProductInfo.do',
        'queryBarcode'          => '/api/openapi/queryBarcodes.do',
        'queryInventoryList'    => '/api/openapi/getInventoryListByGroup.do',
        'queryDeliveryInfo'     => '/api/openapi/getDeliveryInfo.do',
        'createOrder'           => '/api/openapi/setOrderInfos.do',
        'queryOrder'            => '/api/openapi/getOrderInfo.do',
        'applyCancelOrder'      => '/api/openapi/applyCancel.do',
        'addAfterSalesOrder'    => '/api/openapi/addAfterSales.do',
        'getOrderFeedBackList'  => '/api/openapi/getOrderFeedBackList.do',
    ];
    

    public function __construct($apiBody = []){
        $this->config = $apiBody;
    }

    /**
     * 创建订单
     * @param array $params
     * @return mixed
     */
/**
     * 创建订单
     */
    public function createOrder( $params = [] )
    {
        if( empty($params['supplierOrder']) || empty($params['orderGoods']) || empty($params['order']) || empty($params['binfo']) ) {
            return Normal::returnCode(Normal::FAIL,"except param supplierOrder or orderGoods or order or binfo1");
        }
        //组装订单参数
        $orderInfo = $params['supplierOrder'];
        $order     = $params['order'];
        $binfo     = $params['binfo'];
        $supplier_map_list = $params['supplier_map_list'];
        $warehouse_name = '';
        $delivery       = '';
        $goods_info    = [];

        //获取下单商品信息
        foreach ($params['orderGoods'] as $key => $item )
        {
            //商品组
            if(empty($supplier_map_list[$item['supplier_skuid']])){

                return Normal::returnCode(Normal::FAIL,"供应商商品ID".$item['supplier_skuid']."数据不存在，请核实！");
            }

            $supplierInfo   = $supplier_map_list[$item['supplier_skuid']];
            // 仓库名字
            $warehouse_name = $supplierInfo['DefaultStock'];
            //发货方式
            $delivery       = $supplierInfo['DeliveryMethod'];

            $spec           = json_decode($supplierInfo['SupplierCommoditySpec'],true);
            if(!isset($spec[0]["spec_value"]) or empty($spec[0]["spec_value"])){
                return Normal::returnCode(Normal::FAIL,"供应商商品{$supplierInfo['SupplierCommodityKey']}规格属性为空，请核实！");
            }
            
            //商品购买规格数量
            $spec_num           = $item['spec_num'];
            //商品购买数量(下单数量 X 起售数量)
            $quantity           = $item['quantity'];
            //实际购买数量
            $buyAcount          = $spec_num * $quantity ;
            
            $goods_info[]= [
                'goods_no'     => (string)$supplierInfo['SupplierCommodityKey'],
                'size'         => (string)$spec[0]["spec_value"],
                'amount'       => $buyAcount,
            ];
        }
        
        $orders_info = [[
            'order_sn'          => $orderInfo['supplier_dsn'],
            'name'              => $orderInfo["receive_name"],
            'mobile'            => $orderInfo["recveive_phone"],
            'address'           => $orderInfo["receive_address"],
            'province'          => $orderInfo["province"],
            'city'              => $orderInfo["city"],
            'area'              => $orderInfo["district"],
            'delivery'          => $delivery,
            'warehouse_name'    => $warehouse_name,
            'buyer_remark'      => $orderInfo['order_remark']??"尽快发货",
            'goods_info'        => $goods_info,
        ]];
        //创建订单数据
        $data = [
            'sign'          => $this->config['sign'],
            'name'          => $this->config["account"],
            'pwd'           => $this->config["password"],
            'orders_info'   => json_encode($orders_info,JSON_UNESCAPED_UNICODE),
        ];
        $requestParam = [
            'method'    => 'createOrder',
            'data'      => $data,
        ];
        $res  = $this->createOrderExecute($requestParam);
        if( $res['code'] == Normal::SUC ) {
            $thirdpart_order_sn =  $res["data"][0]["orders"][0]["order_id"];
            return Normal::returnCode(Normal::SUC,"订单推送成功",[
                'thirdpart_order_sn' => $thirdpart_order_sn
            ]);
        }
        return $res;
    }
    /**
     * 取消订单
     */
    public function cancelOrder($params = []) {
        $supplier_dsn = $params['supplierOrder']['supplier_dsn'];
        //先查询订单是否反馈成功，成功走售后，不成功走取消
        $data =[
            'sign'          => $this->config['sign'],
            'name'          => $this->config["account"],
            'pwd'           => $this->config["password"],
            'order_sn'      => $supplier_dsn,
            'page'          => 1,
        ];
        $requestParam = [
            'method'    => 'queryOrder',
            'data'      => $data
        ];
        $res = $this->inventoryExecute($requestParam);
        if($res["code"] != Normal::SUC){
            return Normal::returnCode(Normal::FAIL,"售后失败【失败原因：".$res['msg']."】");
        }
        if(!isset($res["data"]["total"]) or $res["data"]["total"] == 0){
            return Normal::returnCode(Normal::FAIL,"售后失败【供应商系统里没有查到此订单，订单号：".$supplier_dsn."】");
        }
        $orderInfo = current($res["data"]["rows"]);
        $orderStatus = $orderInfo["status"];
        //订单状态 0：待付款 10：已付款 30：待配货 40：待反馈 50：反馈成功 60：反馈失败 70：待退货 80:已退货 90：待退款 100：已退款
        if(in_array($orderStatus,[70,80,90,100])){
            return Normal::returnCode(Normal::SUC,"售后成功");
        }
        if(in_array($orderStatus,[0,10,30,40,60])){
            return  $this->applyCancelOrder($params);
        }else{
            return $this->addAfterSalesOrder($params);
        }
    }
    /**
     * 添加售后取消订单
     */
    public function applyCancelOrder($params = []) {
        $supplier_dsn = $params['supplierOrder']['supplier_dsn'];
        $data =[
            'sign'          => $this->config['sign'],
            'name'          => $this->config["account"],
            'pwd'           => $this->config["password"],
            'order_sn'      => $supplier_dsn,
            'qxyy'          => $params["order"]["exceptReason"],
        ];
        $requestParam = [
            'method'    => 'applyCancelOrder',
            'data'      => $data
        ];
        return $this->cancelOrderExecute($requestParam);
    }
    /**
     * 添加售后
     */
    public function addAfterSalesOrder($params = []) {
        $supplier_dsn = $params['supplierOrder']['supplier_dsn'];
        //先查询订单是否反馈成功，成功走售后，不成功走取消
        $data =[
            'sign'              => $this->config['sign'],
            'order_id'          => $params["supplierOrder"]["thirdpart_order_sn"],
            'problem_type'      => "无理由退货",
            'problem_content'   => $params["order"]["exceptReason"],
        ];
        $requestParam = [
            'method'    => 'addAfterSalesOrder',
            'data'      => $data
        ];
        return $this->execute($requestParam);
    }
    
    /**
     * 查询物流单信息
     * @param array $param
     */
    public function queryOrderExpress( $param = [] )
    {
        $data =[
            'sign'          => $this->config['sign'],
            'name'          => $this->config["account"],
            'pwd'           => $this->config["password"],
            'order_sn'      => $param['order_sn'],
        ];
        $requestParam = [
            'method'    => 'getOrderFeedBackList',
            'data'      => $data
        ];
        return $this->execute($requestParam);
    }
    
//     /**
//      * 查询商品信息
//      */
//     public function queryGoodsList($param)
//     {
//         //先根据指定仓拉商品
//         if(!isset($this->config['wareHouseNames'])){
//             return Normal::returnCode(Normal::FAIL,"请指定货源仓库！");
//         }
        
//         $inventoryWhere = [
//             'sign'          => $this->config['sign'],
//             'wareHouseNames'=> $this->config['wareHouseNames'],
//             'page'          => $param['page']  ?? 1,  
//             'rows'          => 200,//(string)$param['limit']  ?? 50,
//             'return_type'   => 1,//货源是否支持退货，1：支持退货 2：不支持退货
//         ];
//         $requestParam = [
//             'method'    => 'queryInventoryList',
//             'data'      => $inventoryWhere,
//         ];
//         $res  = $this->inventoryExecute($requestParam);
//         if($res["code"] != Normal::SUC or !isset($res["data"]["rows"]) or empty($res["data"]["rows"])){
//             return $res;
//         }
//         $result_query_goods = $res["data"]["rows"];
//         $details = [];
//         $wg = new WaitGroup();
//         $count = count($result_query_goods);
//         $wg->add($count);
//         foreach ($result_query_goods as $sku)
//         {
//             co(function () use ($wg,$sku, & $details)
//             {
// //                 print_R($sku);
// //                 //获取市场价：marketprice*discount/10，四舍五入
// //                 $deliveryMethod = "普通快递";
// //                 $pic_url = "";
// //                 if(isset($sku["pic_url"]) && !empty($sku["pic_url"])){
// //                     $pic_url = $sku["pic_url"];
// //                 }
// //                 $marketprice = bcmul($sku["marketprice"],$sku["discount"],2);
// //                 $marketprice = $marketprice >0 ? bcdiv($marketprice,10,2) : 0 ;
// //                 $marketprice = ceil($marketprice);
// //                 $describe    = "";
// //                 $title       = "";
// //                 //查询商品详情
// //                 $detailResponseParam = $this->queryGoodsDetail($sku["articleno"]);
// //                 if ($detailResponseParam['code']== Normal::SUC && isset($detailResponseParam['data']["data"]) && !empty($detailResponseParam['data']["data"])){
// //                     $detailInfo = $detailResponseParam["data"]["data"];
// //                     $describe = isset($detailInfo["describe"]) ? $detailInfo["describe"] : '';
// //                     $title    = $detailInfo["title"];
// //                 }
                
// //                 //通过货源查询快递
// //                 $deliveryResponseParam = $this->getDeliveryInfo($sku["wareHouseName"]);
// //                 if($deliveryResponseParam["code"] == Normal::SUC && isset($deliveryResponseParam['data']["rows"]) && !empty($deliveryResponseParam['data']["rows"]) ){
// //                     //获取首重邮费最低的快递公司
// //                     $deliveryInfo = $deliveryResponseParam['data']["rows"];
// //                     foreach ($deliveryInfo as $d =>$delivery){
// //                         if($delivery["first_postage"]>0){
// //                             $newDeliveryData[$delivery["first_postage"]]=$delivery;
// //                         }
// //                     }
// //                     if(!empty($newDeliveryData)){
// //                         $minKey = min(array_column($newDeliveryData,'first_postage','first_postage'));
// //                         $deliveryMethod = $newDeliveryData[$minKey]["dilivery_name"];
// //                     }
// //                 }
//                 //组装数据
//                 $details[]= [
//                     'articleno'     => $sku["articleno"],//商品货号
//                     'size'          => $sku["size"],
//                     'stock_id'      => $sku["stock_id"],
//                     'wareHouseName' => $sku["wareHouseName"],
//                 ];
//                 $wg->done();
//             });
//         }
//         Tianmagoods::instance()->insert($details);
//         $wg->wait();
//         return Normal::returnCode(Normal::SUC,"获取成功",[
//             'data'      => $details,
//             'has_next'  => true
//         ]);
    
//     }
    
    /**
     * 查询商品信息
     */
//     public function queryGoodsList($param)
//     {
//         //先根据指定仓拉商品
//         if(!isset($this->config['wareHouseNames'])){
//             return Normal::returnCode(Normal::FAIL,"请指定货源仓库！");
//         }
//         $inventoryWhere = [
//             'sign'          => $this->config['sign'],
//             'wareHouseNames'=> $this->config['wareHouseNames'],
//             'page'          => $param['page']  ?? 1,
//             'rows'          => (string)$param['limit']  ?? 50,
//             'minNum'        => 5,
//             'return_type'   => 1,//货源是否支持退货，1：支持退货 2：不支持退货
//         ];
//         if(isset($this->config['articleno'])){
//             $inventoryWhere["articleno"] = $this->config['articleno'];
//         }
//         $requestParam = [
//             'method'    => 'queryInventoryList',
//             'data'      => $inventoryWhere,
//         ];
//         $res  = $this->inventoryExecute($requestParam);
//         if($res["code"] != Normal::SUC or !isset($res["data"]["rows"]) or empty($res["data"]["rows"])){
//             return $res;
//         }
//         $result_query_goods = $res["data"]["rows"];
        
//         $details = [];

//         $size       = $this->config['batchSize'] ??15;
//         $chunkArr   = array_chunk($result_query_goods,$size);
//         foreach ($chunkArr as $skuList){
            
//             $wg = new WaitGroup();
//             $count = count($skuList);
//             $wg->add($count);
//             foreach ($skuList as $sku)
//             {
//                 co(function () use ($wg,$sku, & $details)
//                 {
//                     //获取市场价：marketprice*discount/10，四舍五入
//                     $deliveryMethod = "普通快递";
//                     $pic_url = "";
//                     if(isset($sku["pic_url"]) && !empty($sku["pic_url"])){
//                         $pic_url = $sku["pic_url"];
//                     }
//                     $marketprice = bcmul($sku["marketprice"],$sku["discount"],2);
//                     $marketprice = $marketprice >0 ? bcdiv($marketprice,10,2) : 0 ;
                    
//                     $describe    = "";
//                     $title       = $sku["brandName"].$sku["cate"].$sku["articleno"];//预设默认值
//                     //查询商品详情
//                     $detailResponseParam = $this->queryGoodsDetail($sku["articleno"]);
//                     if ($detailResponseParam['code']== Normal::SUC && isset($detailResponseParam['data']["data"]) && !empty($detailResponseParam['data']["data"])){
//                         $detailInfo = $detailResponseParam["data"]["data"];
//                         $describe = isset($detailInfo["describe"]) ? $detailInfo["describe"] : '';
//                         $title    = $detailInfo["title"];
//                     }
//                     //组装数据
//                     $details[]= [
//                         'articleno'     => (string)$sku["articleno"],//商品货号
//                         'descr'         => $describe,//商品描述信息
//                         'cate'          => (string)$sku["cate"],//商品三级分类
//                         'brandname'     => (string)$sku["brandName"],//品牌
//                         'marketprice'   => (float)$marketprice,
//                         'barCode'       => (string)$sku["articleno"],
//                         'pic_url'       => $pic_url,
//                         'title'         => (string)$title." ".$sku["size"],
//                         'size'          => (string)$sku["size"],
//                         'wareHouseName' => (string)$sku["wareHouseName"],
//                         'ukSize'        => (string)$sku["ukSize"],
//                         'innerNum'      => (int)$sku["innerNum"],
//                         'stock_id'      => (string)$sku["stock_id"],
//                         'deliveryMethod'=> (string)$deliveryMethod,
//                     ];
//                     $wg->done();
//                 });
//             }
//             $wg->wait();
//         }
//         return Normal::returnCode(Normal::SUC,"获取成功",[
//             'data'      => $details,
//             'has_next'  => true
//         ]);
    
//     }
    
    /**
     * 查询商品信息
     */
    public function queryGoodsList($param)
    {
        //先根据指定仓拉商品
        if(!isset($this->config['wareHouseNames'])){
            return Normal::returnCode(Normal::FAIL,"请指定货源仓库！");
        }
        $inventoryWhere = [
            'sign'          => $this->config['sign'],
            'wareHouseNames'=> $this->config['wareHouseNames'],
            'page'          => $param['page']  ?? 1,
            'rows'          => (string)$param['limit']  ?? 50,
            'minNum'        => 5,
            'return_type'   => 1,//货源是否支持退货，1：支持退货 2：不支持退货
        ];
        if(isset($this->config['articleno'])){
            $inventoryWhere["articleno"] = $this->config['articleno'];
        }
        $requestParam = [
            'method'    => 'queryInventoryList',
            'data'      => $inventoryWhere,
        ];
        
        $res = $this->inventoryExecute($requestParam);
        //错误情况
        if( $res["code"] != Normal::SUC ) 
        {
            return Normal::returnCode(Normal::FAIL,$res['msg'],[
                'has_next'  => true,
                'total'     => 0,
                'list'      => []
            ]);
        }
        
        //空数据
        if( empty($res["data"]["rows"]) ) 
        {
            return Normal::returnCode(Normal::SUC,$res['msg'],[
                'has_next'  => false,
                'total'     => 0,
                'list'      => []
            ]);
        }
        
        $result_query_goods = $res["data"]["rows"];
        $total_count = 0;
        if( $inventoryWhere['page'] == 1 ) {
            $total_count = $res["data"]["total"] ?? 0;
        }
        $details = [];
        $size       = $this->config['batchSize'] ??15;
        $chunkArr   = array_chunk($result_query_goods,$size);
        foreach ($chunkArr as $skuList)
        {
            $articleno = array_column($skuList,'articleno');
            $supplierCommodity = SupplierCommodity::instance()->select(['SupplierCommoditySkuCode','SupplierCommodityName','SupplierCommodityKey','SupplierCommodityDescription'])->whereIn("SupplierCommodityKey",$articleno)->get()->toArray();
            $supplierCommodityMap = array_column($supplierCommodity,null,'SupplierCommoditySkuCode');
            $wg = new WaitGroup();
            $count = count($skuList);
            $wg->add($count);
            foreach ($skuList as $sku)
            {
                co(function () use ($wg,$sku, & $details,&$supplierCommodityMap)
                {
                    //获取市场价：marketprice*discount/10，四舍五入
                    $deliveryMethod = "普通快递";
                    $pic_url = "";
                    if(isset($sku["pic_url"]) && !empty($sku["pic_url"])){
                        $pic_url = $sku["pic_url"];
                    }
                    $marketprice = bcmul($sku["marketprice"],$sku["discount"],2);
                    $marketprice = $marketprice >0 ? bcdiv($marketprice,10,2) : 0 ;
    
                    $describe    = "";
                    $title       = $sku["brandName"].$sku["cate"].$sku["articleno"];//预设默认值
                    
                    //如果该商品名存在，则从数据库中获取，否则，就查询商品详情接口
                    if(isset($supplierCommodityMap[$sku["stock_id"]])){
                        $title      = $supplierCommodityMap[$sku["stock_id"]]["SupplierCommodityName"];
                        $describe   = $supplierCommodityMap[$sku["stock_id"]]["SupplierCommodityDescription"];
                        $explodeArr = \explode(' ',$title);
                        array_pop($explodeArr);
                        $title = \implode(" ", $explodeArr);
                    }
                    else
                    {
                        //查询商品详情
                        $detailResponseParam = $this->queryGoodsDetail($sku["articleno"]);
                        if ($detailResponseParam['code']== Normal::SUC && isset($detailResponseParam['data']["data"]) && !empty($detailResponseParam['data']["data"])){
                            $detailInfo = $detailResponseParam["data"]["data"];
                            $describe = isset($detailInfo["describe"]) ? $detailInfo["describe"] : '';
                            $title    = $detailInfo["title"];
                        } 
                    }
                    
                    //组装数据
                    $details[]= [
                        'articleno'     => (string)$sku["articleno"],//商品货号
                        'descr'         => $describe,//商品描述信息
                        'cate'          => (string)$sku["cate"],//商品三级分类
                        'brandname'     => (string)$sku["brandName"],//品牌
                        'marketprice'   => (float)$marketprice,
                        'barCode'       => (string)$sku["articleno"],
                        'pic_url'       => $pic_url,
                        'title'         => (string)$title." ".$sku["size"],
                        'size'          => (string)$sku["size"],
                        'wareHouseName' => (string)$sku["wareHouseName"],
                        'ukSize'        => (string)$sku["ukSize"],
                        'innerNum'      => (int)$sku["innerNum"],
                        'stock_id'      => (string)$sku["stock_id"],
                        'deliveryMethod'=> (string)$deliveryMethod,
                    ];
                    $wg->done();
                });
            }
            $wg->wait();
        }
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'has_next'  => true,
            'list'      => $details,
            'total'     => $total_count    
        ]);
    }

    /**
     * 查询商品详情
     */
    public function queryGoodsDetail($goods_no)
    {
        //查询商品详情
        $detailsWhere = [
            'sign'      => $this->config['sign'],
            'goods_no'  => $goods_no,
        ];
        $requestParam = [
            'method'    => 'queryGoodsDetail',
            'data'      => $detailsWhere,
        ];
        $responseParam = $this->execute($requestParam);
        return $responseParam;
         
    }
    

    /**
     * 查询仓库对应的快递
     */
    public function getDeliveryInfo($wareHouseName)
    {
        $deliveryWhere = [
            'sign'          => $this->config['sign'],
            'wareHouseName' => $wareHouseName,
            'page'          => 1,
            'rows'          => 50,
        ];
        $requestParam = [
            'method'    => 'queryDeliveryInfo',
            'data'      => $deliveryWhere,
        ];
        $responseParam = $this->inventoryExecute($requestParam);
        return $responseParam;
         
    }
    /**
     * CURL请求
     * @param $params
     * @return mixed
     */
    public function createOrderExecute( $param = [] ){
    
        $method = $param["method"];
        $url    = $this->config["baseurl"].$this->RouteMap[$method];
        $parameter = [
            [
                'tag'    => 'Shoptianma_' . $method,
                'uid'    => IdService::instance()->getOtherID(),
                'url'    => $url,
                'data'   => $param['data'],
                'method' => "post",
                'format' => '',
                'header' => []
            ]
        ];
        $resTask = TaskWorker::instance()->addTask($parameter);
        $resTask   = current($resTask);
        $response  = $resTask['response']['result'];
        $result    = @json_decode($response, true);
        if (isset($result[0]['status']) && $result[0]['status'] == 0) {
            return Normal::returnCode(Normal::SUC, "OK",$result);
        }
        $message = isset($result[0]['info']) ? $result[0]["info"] : "";
        $message = $message ?? $resTask["error_info"];
        $message = $message ?? '操作失败';
        return Normal::returnCode(Normal::FAIL, $message);
    }
    /**
     * CURL请求
     * @param $params
     * @return mixed
     */
    public function execute( $param = [] ){
        
        $method = $param["method"];
        $url    = $this->config["baseurl"].$this->RouteMap[$method];
        $parameter = [
            [
                'tag'    => 'Shoptianma_' . $method,
                'uid'    => IdService::instance()->getOtherID(),
                'url'    => $url,
                'data'   => $param['data'],
                'method' => "post",
                'format' => '',
                'header' => []
            ]
        ];
        
        if( in_array($method,['queryGoodsList','queryGoodsDetail']) ) {
            $resTask = TaskWorker::instance()->addTask($parameter,'handleDiableTask');
        } else {
            $resTask = TaskWorker::instance()->addTask($parameter);
        }
        $resTask   = current($resTask);
        $response  = $resTask['response']['result'];
        $result    = @json_decode($response, true);
        if (!isset($result['error_code']) or $result['error_code'] == 0) {
            return Normal::returnCode(Normal::SUC, "OK",$result);
        }
        $message = $result['error_info'] ?? '操作失败';
        return Normal::returnCode(Normal::FAIL, $message);
    }
    
    /**
     * CURL请求
     * @param $params
     * @return mixed
     */
    public function inventoryExecute( $param = [] ){
    
        $method = $param["method"];
        $url    = $this->config["baseurl"].$this->RouteMap[$method];
        $parameter = [
            [
                'tag'    => 'Shoptianma_' . $method,
                'uid'    => IdService::instance()->getOtherID(),
                'url'    => $url,
                'data'   => $param['data'],
                'method' => "post",
                'format' => '',
                'header' => []
            ]
        ];
        $resTask = TaskWorker::instance()->addTask($parameter,'handleDiableTask');
        $resTask   = current($resTask);
        $response  = $resTask['response']['result'];
        $result    = @json_decode($response, true);
        if (isset($result['total'])) {
            return Normal::returnCode(Normal::SUC, "OK",$result);
        }
        $message = $result['msg'] ?? '操作失败';
        return Normal::returnCode(Normal::FAIL, $message);
    }
    
    /**
     * CURL请求
     * @param $params
     * @return mixed
     */
    public function cancelOrderExecute( $param = [] ){
    
        $method = $param["method"];
        $url    = $this->config["baseurl"].$this->RouteMap[$method];
        $parameter = [
            [
                'tag'    => 'Shoptianma_' . $method,
                'uid'    => IdService::instance()->getOtherID(),
                'url'    => $url,
                'data'   => $param['data'],
                'method' => "post",
                'format' => '',
                'header' => []
            ]
        ];
        $resTask = TaskWorker::instance()->addTask($parameter);
        $resTask   = current($resTask);
        $response  = $resTask['response']['result'];
        $result    = @json_decode($response, true);
        if (!isset($result['error_code']) or $result['error_code'] == 0) {
            if(isset($result["data"][0]["status"]) && !empty($result["data"][0]["status"]) && $result["data"][0]["status"] == 0){
                return Normal::returnCode(Normal::SUC, "订单取消成功",$result);
            }else{
                return Normal::returnCode(Normal::FAIL, "订单取消失败",$result);
            }
        }
        $message = $result['error_info'] ?? '订单取消失败';
        return Normal::returnCode(Normal::FAIL, $message);
    }
}